package com.example.task1.ui.AllService.YouthStation;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PersonBean;
import com.example.task1.bean.RegionBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class PersonActivity extends AppCompatActivity {

    private ImageView image;
    private TextView tvContent;
    private RecyclerView rvList;
    private int id;
    private ActionBar supportActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        initView();
        supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        initData();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/youth-inn/talent-policy-area/" + id);
        Api.send(apiRespCall, RegionBean.class, new CallBack<RegionBean>() {
            @Override
            public void success(RegionBean data) {
                RegionBean.DataDTO rows = data.getData();
                MyApplication.glide(image, rows.getImgUrl());
                tvContent.setText(rows.getIntroduce());
                supportActionBar.setTitle(rows.getName() + "政策区");
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> Call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/youth-inn/talent-policy/list?areaId=" + id);
        Api.send(Call, PersonBean.class, new CallBack<PersonBean>() {
            @Override
            public void success(PersonBean data) {
                PersonAdapter personAdapter = new PersonAdapter(data.getData(),PersonActivity.this);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PersonActivity.this);
                rvList.setAdapter(personAdapter);
                rvList.setLayoutManager(linearLayoutManager);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initView() {
        image = (ImageView) findViewById(R.id.image);
        tvContent = (TextView) findViewById(R.id.tv_content);
        rvList = (RecyclerView) findViewById(R.id.rv_list);
    }
}